<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"foryoungyu.gitee.io","root":"/","scheme":"Gemini","version":"8.0.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="MySQL初次安装常见问题Failed to find valid data directory.Linux 安装Mysql或者Mariadb后，想要开启数据库服务，一般会打开服务失败。 输入systemctl status mysqld.service之后输出入下： 12345678910111213141516171819202122● mysqld.service - MySQL Serve">
<meta property="og:type" content="article">
<meta property="og:title" content="【数据库】MySQL初次安装常见问题">
<meta property="og:url" content="https://foryoungyu.gitee.io/2021/02/13/%E3%80%90%E6%95%B0%E6%8D%AE%E5%BA%93%E3%80%91Mysql%E5%88%9D%E6%AC%A1%E5%AE%89%E8%A3%85%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/index.html">
<meta property="og:site_name" content="Salt.&#39;s Blog">
<meta property="og:description" content="MySQL初次安装常见问题Failed to find valid data directory.Linux 安装Mysql或者Mariadb后，想要开启数据库服务，一般会打开服务失败。 输入systemctl status mysqld.service之后输出入下： 12345678910111213141516171819202122● mysqld.service - MySQL Serve">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-02-12T16:00:00.000Z">
<meta property="article:modified_time" content="2021-02-14T03:36:52.482Z">
<meta property="article:author" content="Foryoung Yu">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://foryoungyu.gitee.io/2021/02/13/%E3%80%90%E6%95%B0%E6%8D%AE%E5%BA%93%E3%80%91Mysql%E5%88%9D%E6%AC%A1%E5%AE%89%E8%A3%85%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>【数据库】MySQL初次安装常见问题 | Salt.'s Blog</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Salt.'s Blog</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">生于忧患，死于安乐</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-系列">

    <a href="/series/" rel="section"><i class="fa fa-book fa-fw"></i>系列</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#MySQL%E5%88%9D%E6%AC%A1%E5%AE%89%E8%A3%85%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98"><span class="nav-number">1.</span> <span class="nav-text">MySQL初次安装常见问题</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Failed-to-find-valid-data-directory"><span class="nav-number">1.1.</span> <span class="nav-text">Failed to find valid data directory.</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ER-NOT-SUPPORTED-AUTH-MODE-Client-does-not-support-authentication-protocol-requested-by-server"><span class="nav-number">1.2.</span> <span class="nav-text">ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server</span></a></li></ol></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Foryoung Yu"
      src="/images/dog.jpg">
  <p class="site-author-name" itemprop="name">Foryoung Yu</p>
  <div class="site-description" itemprop="description">欣欣向荣</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">9</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/ForyoungYu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ForyoungYu" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:foryoung@outlook.com" title="E-Mail → mailto:foryoung@outlook.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/5935274514" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;5935274514" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
  </div>



      </section>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://foryoungyu.gitee.io/2021/02/13/%E3%80%90%E6%95%B0%E6%8D%AE%E5%BA%93%E3%80%91Mysql%E5%88%9D%E6%AC%A1%E5%AE%89%E8%A3%85%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/dog.jpg">
      <meta itemprop="name" content="Foryoung Yu">
      <meta itemprop="description" content="欣欣向荣">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Salt.'s Blog">
    </span>

    
    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          【数据库】MySQL初次安装常见问题
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-02-13 00:00:00" itemprop="dateCreated datePublished" datetime="2021-02-13T00:00:00+08:00">2021-02-13</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-02-14 11:36:52" itemprop="dateModified" datetime="2021-02-14T11:36:52+08:00">2021-02-14</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" itemprop="url" rel="index"><span itemprop="name">数据库</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="MySQL初次安装常见问题"><a href="#MySQL初次安装常见问题" class="headerlink" title="MySQL初次安装常见问题"></a>MySQL初次安装常见问题</h1><h2 id="Failed-to-find-valid-data-directory"><a href="#Failed-to-find-valid-data-directory" class="headerlink" title="Failed to find valid data directory."></a>Failed to find valid data directory.</h2><p>Linux 安装Mysql或者Mariadb后，想要开启数据库服务，一般会打开服务失败。</p>
<p>输入<code>systemctl status mysqld.service</code>之后输出入下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">● mysqld.service - MySQL Server</span><br><span class="line">     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)</span><br><span class="line">     Active: failed (Result: exit-code) since Sat 2021-02-13 20:53:24 CST; 19min ago</span><br><span class="line">       Docs: man:mysqld(8)</span><br><span class="line">             http://dev.mysql.com/doc/refman/en/using-systemd.html</span><br><span class="line">    Process: 422056 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)</span><br><span class="line">    Process: 422124 ExecStart=/usr/bin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)</span><br><span class="line">   Main PID: 422124 (code=exited, status=1/FAILURE)</span><br><span class="line">     Status: &quot;Server startup in progress&quot;</span><br><span class="line">      Error: 2 (没有那个文件或目录)</span><br><span class="line"></span><br><span class="line">2月 13 20:53:23 arch mysqld_pre_systemd[422082]: 2021-02-13T12:53:23.566434Z 0 [System] [MY-010910] [Server] /usr/bin/mysqld: Shutdown complete (mysqld 8.0.23)  Source distribution.</span><br><span class="line">2月 13 20:53:24 arch mysqld[422124]: 2021-02-13T12:53:24.367415Z 0 [Warning] [MY-010915] [Server] &#x27;NO_ZERO_DATE&#x27;, &#x27;NO_ZERO_IN_DATE&#x27; and &#x27;ERROR_FOR_DIVISION_BY_ZERO&#x27; sql modes should be used with strict mode. They will be merged with strict mode in a future release.</span><br><span class="line">2月 13 20:53:24 arch mysqld[422124]: 2021-02-13T12:53:24.367480Z 0 [System] [MY-010116] [Server] /usr/bin/mysqld (mysqld 8.0.23) starting as process 422124</span><br><span class="line">2月 13 20:53:24 arch mysqld[422124]: 2021-02-13T12:53:24.454185Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.</span><br><span class="line">2月 13 20:53:24 arch mysqld[422124]: 2021-02-13T12:53:24.454521Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.</span><br><span class="line">2月 13 20:53:24 arch mysqld[422124]: 2021-02-13T12:53:24.454706Z 0 [ERROR] [MY-010119] [Server] Aborting</span><br><span class="line">2月 13 20:53:24 arch mysqld[422124]: 2021-02-13T12:53:24.455860Z 0 [System] [MY-010910] [Server] /usr/bin/mysqld: Shutdown complete (mysqld 8.0.23)  Source distribution.</span><br><span class="line">2月 13 20:53:24 arch systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE</span><br><span class="line">2月 13 20:53:24 arch systemd[1]: mysqld.service: Failed with result &#x27;exit-code&#x27;.</span><br><span class="line">2月 13 20:53:24 arch systemd[1]: Failed to start MySQL Server.</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>发现第一个ERROR的错误是<code>Failed to find valid data directory.</code>。经过查阅资料发现，说的就是初始化Data文件夹失败了。而这个Data文件夹指的就是/etc/my.cnf下面配置的datadir路径。</p>
<p><strong>my.cnf</strong> 如果不在/etc目录下，可在/etc/mysql目录下复制一份到/etc目录下。</p>
<p>在my.cnf文件中有这么一项参数：datadir=/var/lib/mysql </p>
<p>移动到/var/lib目录下将mysql目录删除掉：<code>rm -rf /var/lib/mysql</code></p>
<p>之后重新初始化：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> mysqld --initialize --console</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> chown -R mysql:mysql /var/lib/mysql/</span></span><br></pre></td></tr></table></figure>

<p>执行完成之后自动生成一串root密码，记住他。</p>
<p>之后就可以正常启动mysql了</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> systemctl start mysqld</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> systemctl <span class="built_in">enable</span> mysqld</span></span><br></pre></td></tr></table></figure>

<p>重设mysql的root密码，执行<code>mysql_secure_installation</code>输入刚刚生成的root密码。</p>
<p>之后根据需要选择y或n即可。</p>
<h2 id="ER-NOT-SUPPORTED-AUTH-MODE-Client-does-not-support-authentication-protocol-requested-by-server"><a href="#ER-NOT-SUPPORTED-AUTH-MODE-Client-does-not-support-authentication-protocol-requested-by-server" class="headerlink" title="ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server"></a>ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server</h2><p>导致这个错误的原因是，目前，最新的mysql模块并未完全支持MySQL 8的“caching_sha2_password”加密方式，而“caching_sha2_password”在MySQL 8中是默认的加密方式。因此，下面的方式命令是默认已经使用了“caching_sha2_password”加密方式，该账号、密码无法在mysql模块中使用。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">mysql&gt;</span><span class="bash"> ALTER USER <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;localhost&#x27;</span> IDENTIFIED BY <span class="string">&#x27;123456&#x27;</span>;</span></span><br><span class="line">Query OK, 0 rows affected (0.12 sec)</span><br></pre></td></tr></table></figure>

<p><strong>解决方法</strong></p>
<p>解决方法是从新修改用户root的密码，并指定mysql模块能够支持的加密方式：</p>
<figure class="highlight plain"><figcaption><span>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Query OK, 0 rows affected (0.12 sec)</span><br></pre></td></tr></table></figure>
<p>上述语句，显示指定了使用“mysql_native_password”的加密方式。这种方式是在mysql模块能够支持。</p>
<blockquote>
<p>参考文献：</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/liurui50/article/details/105478422">CentOS7 MySQL8.0 启动失败 Data Dictionary initialization failed</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Linux/" rel="tag"># Linux</a>
              <a href="/tags/MySQL/" rel="tag"># MySQL</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/01/10/%E3%80%90LaTex%E3%80%91LaTex%E7%9A%84%E5%AE%89%E8%A3%85%E5%92%8C%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/" rel="prev" title="【LaTex】LaTex的安装和基本使用">
                  <i class="fa fa-chevron-left"></i> 【LaTex】LaTex的安装和基本使用
                </a>
            </div>
            <div class="post-nav-item">
            </div>
          </div>
    </footer>
  </article>
  
  
  



      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Foryoung Yu</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  


















  








  

  

</body>
</html>
